package o2o.web.actions.sys;

import com.jinyou.utils.common.AjaxResult;
import o2o.cache.user.UserInfoMemory;
import o2o.cache.user.UserOnlineImpl;
import o2o.config.dao.UserDaoSql;
import o2o.dao.PO.PojoTables;
import o2o.dao.PO.common.UserType;
import o2o.dao.PO.order.GRAB_ORDER_MODE;
import o2o.dao.PO.sys.SysCityAdmin;
import o2o.dao.PO.user.UserInfo;
import o2o.system.SysSettings;
import o2o.system.data.SYS_SETTINGS;
import o2o.web.actions.company.SysCityCompanyMan;
import o2o.web.actions.user.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.util.List;

@RestController
public class GrabOrderModeController {

    // 修改抢单模式
    @RequestMapping("/admin/grab-order-mode/modify")
    public String updateGrabOrderMode(String token, @RequestParam(defaultValue = "0") Integer grabOrderMode) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        if (grabOrderMode != GRAB_ORDER_MODE.GRAB_DISPATCH_MODE
                && grabOrderMode != GRAB_ORDER_MODE.ONLY_GRAB_MODE
                && grabOrderMode != GRAB_ORDER_MODE.ONLY_DISPATCH_MODE
                && grabOrderMode != GRAB_ORDER_MODE.DISPATCH_FIRST_TO_OTHER
        ) {
            return AjaxResult.errorResult("操作模式不正确");
        }
        if (!User.getInstance().hasSuperAdminAuth(user.getUserType())) {
            return AjaxResult.errorResult("您没有访问权限");
        }
        StringBuffer sb = new StringBuffer();
        sb.append("update ").append(PojoTables.SYS_SETTING).append(" set val='").append(grabOrderMode).append("'")
                .append(",updateUser='").append(user.getUsername()).append("'");
        sb.append(" where delFlag = 0 and code = '").append("grabOrderMode").append("'");
        try {
            UserDaoSql.getInstance().executeBySql(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("修改失败，请稍后再试");
        }
        //修改缓存
        SysSettings.init();//重新初始化系统值
        grabOrderMode = SYS_SETTINGS.GRAB_ORDER_MODE;
        return AjaxResult.successResult("" + grabOrderMode);
    }

}
